//
//  EFAUtySelCommonVC.m
//  efairy
//
//  Created by XiangDongMBP on 2017/8/30.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAUtySelCommonVC.h"

@interface EFAUtySelCommonVC ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (assign, nonatomic) NSInteger start;
@property (assign, nonatomic) NSInteger end;
@property (copy,   nonatomic) NSString *unit;
@property (assign, nonatomic) BOOL containsZero;

@end

@implementation EFAUtySelCommonVC

#pragma mark - life cycle
- (instancetype)init {
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end unit:(NSString *)unit containsZero:(BOOL)containsZero {
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        _start = start;
        _end = end;
        _unit = [unit copy];
        _containsZero = containsZero;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initLogicData];
    [self initNavigationBar];
    [self initSubViews];
}
- (void)initLogicData {
    
}
- (void)initNavigationBar {
    
}

- (void)initSubViews {
    [self.cancelBtn addTarget:self action:@selector(cancelBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.sureBtn addTarget:self action:@selector(sureBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.end - self.start + 1 + (self.containsZero ? 1 : 0);
}
#pragma mark - UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* tView = (UILabel*)view;
    if (!tView){
        tView = [[UILabel alloc] init];
        tView.font = [UIFont systemFontOfSize:14.0];
        tView.textAlignment = NSTextAlignmentCenter;
    }
    NSInteger value = 0;
    if (self.containsZero) {
        if (row != 0) {
            value = self.start + row - 1;
        }
    } else {
        value = self.start + row;
    }
    NSString *str = [NSString stringWithFormat:@"%ld%@", (long)value, self.unit];
    tView.text = str;
    return tView;
}

#pragma mark - event response
- (void)cancelBtnTapped:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)sureBtnTapped:(UIButton *)btn {
    NSInteger index0 = [self.pickerView selectedRowInComponent:0];
    NSInteger value = 0;
    if (self.containsZero) {
        if (index0 != 0) {
            value = self.start + index0 - 1;
        }
    } else {
        value = self.start + index0;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.successHandle) {
            self.successHandle(value);
            self.successHandle = nil;
        }
    }];
}

#pragma mark - private methods
#pragma mark - network request
#pragma mark - getters and setters


@end
